package cn.edu.xmu.oomall.payment.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.payment.microservice.bo.NotifyBody;
import cn.edu.xmu.oomall.payment.microservice.vo.PaymentNotifyRetVo;
import cn.edu.xmu.oomall.payment.microservice.vo.RefundNotifyRetVo;
import cn.edu.xmu.oomall.payment.model.vo.PaymentPatternVo;
import cn.edu.xmu.oomall.payment.model.vo.PaymentVo;
import cn.edu.xmu.oomall.payment.service.PaymentService;
import cn.edu.xmu.oomall.payment.util.PaymentCommon;
import cn.edu.xmu.oomall.payment.util.PaymentReturnNo;
import cn.edu.xmu.oomall.payment.util.PaymentReturnObject;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

@Api(value = "支付", tags = "payment")
@RestController
@RefreshScope
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class PaymentController {
    @Autowired
    private HttpServletResponse httpServletResponse;
    @Autowired
    private PaymentService paymentService;

    /**
     * 顾客支付已建立的支付单
     */
    @Audit
    @PutMapping("/payments/{pid}/pay")
    public Object updatePayment(@PathVariable Long pid, @RequestBody PaymentPatternVo patternVo, @LoginUser Long userId, @LoginName String userName, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.FIELD_NOTVALID,"传入的RequestBody参数格式不合法"));
        }
        ReturnObject ret=paymentService.updatePayment(pid,patternVo,userId,userName);
        return Common.decorateReturnObject(ret);
    }


    /**
     * 对账
     */
    @Audit
    @GetMapping("/payments")
    public Object reconciliation(@RequestParam(name = "beginTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") LocalDateTime beginTime,
                                 @RequestParam(name = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") LocalDateTime endTime,
                                 @LoginUser Long userId, @LoginName String userName){

        ReturnObject ret=paymentService.reconciliation(beginTime,endTime,userId,userName);
        return Common.decorateReturnObject(ret);
    }

    /**
     * 顾客支付
     */
    @Audit
    @PostMapping("/payments")
    public Object createPayment(@RequestBody PaymentVo paymentVo, @LoginUser Long userId, @LoginName String userName, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.FIELD_NOTVALID,"传入的RequestBody参数格式不合法"));
        }
        System.out.println("支付");
        ReturnObject ret=paymentService.createPayment(paymentVo,userId,userName);
        if (ret.getCode().equals(ReturnNo.OK)) {
            httpServletResponse.setStatus(201);
        }
        return Common.decorateReturnObject(ret);
    }

    /**
     * 内部API-指定顾客支付
     */
    @Audit
    @PostMapping("/internal/payments")
    public Object createPaymentCustomer(@RequestBody PaymentVo paymentVo, @LoginUser Long userId, @LoginName String userName, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.FIELD_NOTVALID,"传入的RequestBody参数格式不合法"));
        }
        ReturnObject ret=paymentService.createPayment(paymentVo,userId,userName);
        return Common.decorateReturnObject(ret);
    }

    /**
     * 微信支付通知API
     */
    @Audit
    @PostMapping("/wechat/payment/notify")
    public Object wechatPaymentNotify(@RequestBody PaymentNotifyRetVo paymentNotifyVo, @LoginUser Long userId, @LoginName String userName, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return PaymentCommon.decorateReturnObject(new PaymentReturnObject(PaymentReturnNo.FAIL));
        }
        // Todo: 阿里异步通知
        // PaymentReturnObject ret=paymentService.wechatPaymentNotify(paymentNotifyVo,userId,userName);
        PaymentReturnObject ret = new PaymentReturnObject();
        return PaymentCommon.decorateReturnObject(ret);
    }

    /**
     * 微信退款通知API
     */
    @PostMapping("/wechat/refund/notify")
    public Object WechatRefundNotify(@RequestBody RefundNotifyRetVo refundNotifyRetVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return PaymentCommon.decorateReturnObject(new PaymentReturnObject(PaymentReturnNo.FAIL));
        }
        PaymentReturnObject ret=paymentService.WechatRefundNotify(refundNotifyRetVo);
        return PaymentCommon.decorateReturnObject(ret);
    }

    /**
     * 阿里异步支付通知
     */
    @PostMapping("/alipay/notify")
    public Object aliPaymentNotify(@RequestBody NotifyBody notifyBody,  BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return PaymentCommon.decorateReturnObject(new PaymentReturnObject(PaymentReturnNo.FAIL));
        }
//        PaymentReturnObject ret=paymentService.aliPaymentNotify(notifyBody);
        // Todo: 阿里异步通知
        PaymentReturnObject ret = new PaymentReturnObject();
        return PaymentCommon.decorateReturnObject(ret);
    }

    /**
     * 查询自己订单的支付信息
     * @param id
     * @return Object
     */
    @Audit
    @GetMapping("orders/{id}/payment")
    public Object getOrderPayments(@PathVariable Long id) {
        return Common.decorateReturnObject(paymentService.getOrderPayments(id));
    }


    /**
     * 获得支付渠道的所有状态
     * @return Object
     */
    @GetMapping("paypatterns/states")
    public Object getAllPatternStates() {
        return Common.decorateReturnObject(paymentService.getAllPatternStates());
    }


    /**
     * 获取退款单的所有状态
     * @return Object
     */
    @GetMapping("refund/states")
    public Object getAllRefundStates() {return Common.decorateReturnObject(paymentService.getAllRefundStates());}

    /**
     * 获得支付单的所有状态
     * @return Object
     */
    @GetMapping("payment/states")
    public Object getAllPaymentsStates() {
        return Common.decorateReturnObject(paymentService.getAllPaymentsStates());
    }


    /**
     * 获得所有的支付渠道
     * @param shopId
     * @return Object
     */
    @Audit
    @GetMapping("shops/{shopId}/paypatterns")
    public Object getPayPatterns(@PathVariable Long shopId,
                                 @LoginUser Long userId,
                                 @LoginName String userName)
    {
        //对输入数据进行合法性判断
        if(shopId!=0) {
            return Common.decorateReturnObject(new ReturnObject<>(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }

        return Common.decorateReturnObject(paymentService.getPayPatterns());
    }

    /**
     * 获得当前有效的支付渠道
     * @return Object
     */

    @Audit
    @GetMapping("paypatterns")
    public Object getAvailablePatterns(@LoginUser Long userId,
                                       @LoginName String userName) {

        return Common.decorateReturnObject(paymentService.getAvailablePatterns());
    }



}




